package com.dhms.vostok1.data.spot_patrol

import com.dhms.uikit.select_bottom_sheet.SelectItem
import com.squareup.moshi.Json
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey


// 任务类型
enum class PlanType(val code: Int) {
    UNKNOWN(-1),

    //交接班
    RELIEF(1),

    //点巡检
    SPOT_PATROL(2);

    companion object {
        fun fromInt(type: Int) = values()
            .associateBy(PlanType::code)[type] ?: UNKNOWN
    }
}


//任务状态
enum class TaskStatus(val code: Int) {
    UNKNOWN(-1),

    //未开始
    UN_START(1),

    //超时
    TIMEOUT(2),

    //完成
    DONE(3),

    //进行中
    DOING(4),

    //超时完成
    TIMEOUT_DONE(5);

    companion object {
        fun fromInt(type: Int) = values().associateBy(TaskStatus::code)[type] ?: UNKNOWN
    }
}

class TaskGroupData: RealmObject {
    @PrimaryKey
    @Json(name = "group_id")
    var groupID: String? = null

    @Json(name = "plan_name")
    var planName: String? = null

    @Json(name = "plan_principal_id")
    var planPrincipalId: Long? = null

    @Json(name = "plan_type")
    var planType: PlanType? = null

    @Json(name = "task_start_time")
    var taskStartTime: String? = null

    @Json(name = "task_end_time")
    var taskEndTime: String? = null

    @Json(name = "task_complete_time")
    var taskCompleteTime: String? = null

    var code: Int = TaskStatus.UNKNOWN.code
    @Json(name = "task_group_status")
    var taskGroupStatus: TaskStatus?
        get() {
            return try {
                TaskStatus.fromInt(code)
            } catch (e: IllegalArgumentException) {
                TaskStatus.UNKNOWN
            }
        }
        set(value) {
            code = value?.code?:TaskStatus.UNKNOWN.code
        }


    @Json(name = "task_list")
    var taskList: RealmList<TaskData> = realmListOf()

    @Json(name = "team_persons")
    var teamPersonData: RealmList<SelectItem>? = realmListOf()
}


